<?php
include_once 'CF_TableLookup.php';
/**
 * CFtable is a class to simplfy creation of Databse CRUD (Create Remove Update Delete) Tables
* @package CFtable
 * @author Chris Tyler
 * @version 1.0
 * @CFtable is the base class for the an extensable HTML table class for array based data.  This provides data display, sorting, filtering, paging and heading functions
 * @copyright 2007-2008 
 */

class CF_TableLookup extends CF_TableBasic{


	/**
	* @var string $referstring :  A string to tag along to any pages that are called.  not tested
	*/
	var $referstring;	

	/**
	* @var array ar_lookups:  array of arrays (named by $column) of lookup arrays.  
	* Each lookup array has form $lookup[<n>]['keyvalue'] = <key>, $lookup['showvalue']=<thing to show>
	* where n starts at 0
	*/	
	var $ar_lookups = array();
	// make an index by keyvalue of ar_lookups
	var $ar_index_lookups = array();
	

	/**
	* @var string $editvarprefix prefix for filtered variables (column name used)
	*/
	var $editvarprefix = '';


	// ----------------------------------- Variables changed by functions (might make these private someday...)
	
	/**
	* @var string $keys_url_edit Internal String used to show keys and values as row pamamerter
	*/	
    var $keys_url_edit;
	/**
	* @var string $keys_url_edit Internal String used to show keys and values as row pamamerter
	*/
    var $keys_url_act;

	/**
	* @var string $keys_uid keys as comma delimited fields per row
	*/    
	var $keys_uid;

	

// ---------------------------------FUNCTIONS---------------------------------
// ---------------------------------Public Functions--------------------------

	
	/**
	 * setLookups(array $lookups) : This sets the lookup arrays to an array of arrays for each column
	 * @param array $lookups:  The array of arrays, by column.  Each array has form
     * Each lookup array has form $lookup[<n>]['keyvalue'] = <key>, $lookup['showvalue']=<thing to show>
	 * where n starts at 0
	 *
	 */
	function setLookups($lookups){
		foreach($lookups AS $column=>$lookup){
			$this->setLookup($column,$lookup);
		}
	}
	
	/**
	 * setLookup($column,$lookup)
	 * @param string $column:  The name of the column for the lookup
	 * @param array $lookup:  The lookup array
	 * Each lookup array has form $lookup[<n>]['keyvalue'] = <key>, $lookup['showvalue']=<thing to show>
	 * where n starts at 0
	 *
	 */	 
	function setLookup($column,$lookup){
	//print_r($lookup);
		foreach($lookup AS $row=>$pair){
			$index[$lookup[$row]['keyvalue']] = $lookup[$row]['showvalue'];
		}
		$this->ar_index_lookups[$column] = $index;
		$this->ar_lookups[$column]  = $lookup;
		
	}
	function set_lookup($column,$lookup){
		$this->setLookup($column,$lookup);
	}
	

   	/**
	 * set_ordervar The variable to use for specifying column to order by  
	 * @param string $newordervar
	 */		
	function set_ordervar($newordervar){
		$this->ordervar = $newordervar;
	}	
	


	// Also in CF_Multiselect
   	/**
	 *  make_keyvariables  Method will output a comma delimited list of variables (columns) which are keys from fieldspec
	 */
	function make_keyvariables(){
		$aftercomma = '';
		foreach($this->fieldspec as $col_name => $colspec){
			if(!empty($this->fieldspec[$col_name]['key'] )){
				$keyvariables .= $aftercomma . $col_name;
				$aftercomma = ',';
			}
		}
		return $keyvariables;
	}
	



	
	function make_row_end($row_num){
		return "</tr>\n";
	}
	
	/*  disabled since using makefunction for lookups. this gets rid of select dependancy
	function make_rowval($value,$column){
		$row_text = '';
		$row_text .= "<td>";
		// substitute if type = dropdown (D) and looukp array is set
		if(($this->fieldspec[$column]['select'] == 'D') AND (is_array($this->ar_index_lookups[$column]))){
			$row_text .= $this->ar_index_lookups[$column][$value];
		}else{
			$row_text .= $value;
		}
		$row_text .= "</td>";
		return $row_text;
			
	}
	*/
	
	function make_lookup_val($value, $column){
		return $this->ar_index_lookups[$column][$value];
	}

	function clear_keys_strings() { 
		$this->keys_url_edit = '';
        $this->keys_url_act = '';
		$this->keys_uid = '';
	}

	
	/**
	 * function to make key string.  Override / extend
	 * @param int $col_name This is the column name for the key value
	 * @param string $currentvalue This is the value of the key
	 * @param int $firstrow This is 1 for the first row, 0 afterwards for special punctuation
	 * @return string keystring
	 */	
    function make_key_string($col_name, $currentvalue, $firstrow = 0){
		if(empty($this->keys_uid)){
			$this->keys_uid =  "$currentvalue";
	
		}
		else{
			$this->keys_uid .=  ",$currentvalue";
		}
		$this->keys_hidden .= "<input type='hidden' name='$col_name' value='$currentvalue'>";
		//parent::make_key_string($col_name, $currentvalue);
		$this->keys_url_edit .= "&$this->editvarprefix" . "$col_name=$currentvalue";
        $this->keys_url_act .= "&$col_name=$currentvalue";
		
	}
	
}


?>